package com.example.flower_shop.config;

import com.example.flower_shop.websocket.MyWebsocketHandler;
import com.example.flower_shop.websocket.WebSocketInterceptor;
import jakarta.annotation.Resource;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;

/**
 * websocket配置类
 */
@Configuration
@EnableWebSocket
public class WebsocketConfig implements WebSocketConfigurer {
    @Resource
    private MyWebsocketHandler websocketHandler;
    @Resource
    private WebSocketInterceptor webSocketInterceptor;

    /**
     * 端点自动发现器
     * 用于注解式websocket @ServerEndpoint的自动发现
     *
     * @return
     */
//    @Bean
//    public ServerEndpointExporter serverEndpointExporter() {
//        return new ServerEndpointExporter();
//    }
    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        registry.addHandler(websocketHandler, "/ws")
                .addInterceptors(webSocketInterceptor)
                .setAllowedOriginPatterns("*");
//                .withSockJS();
    }
}
